﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp
{
    internal class DemoEvent
    {
        //事件需要在类中声明和触发，并通过委托与事件处理程序关联
        static void Main(string[] args)
        {
            DemoPublisher e = new DemoPublisher(); /* 实例发布器类*/
            DemoSubscriber v = new DemoSubscriber(); /* 实例订阅器类 */
            e.CustomEvent += new DemoPublisher.CustomEntrust(v.printf);
            e.SetValue("C语言中文网");
        }

        /***********发布器类***********/
        public class DemoPublisher
        {
            private string value;
            //声明一个委托类型
            public delegate void CustomEntrust(string str);
            //声明事件本身
            public event CustomEntrust CustomEvent;
            public void SetValue(string s)
            {
                value = s;
                CustomEvent(value);     // 触发事件
            }
        }

        /***********订阅器类***********/
        public class DemoSubscriber
        {
            public void printf(string str)
            {
                Console.WriteLine(str);
            }
        }
    }
}
